import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_learn/main_tab.dart';
import 'package:flutter_learn/router/context_provider.dart';
import 'package:provider/provider.dart';

import 'app/my_app_context.dart';
import 'router/init_router.dart';
import 'utils/init_providers.dart';
import 'utils/provider_util.dart';

void main() {
  runZonedGuarded(() {
    MyAppContext.of().init().then((value) {
      runApp(MultiProvider(providers: providers, child: const MyApp()));
    });
  }, (error, stack) {
    //异常捕获打印
    print(error.toString());
  });
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      routerConfig: InitRouter.instance.router,
      theme: ThemeData(primarySwatch: Colors.red, splashColor: Colors.transparent, highlightColor: Colors.transparent),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    final counter = Provider.of<MyProviderModel>(context);
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              '计数器:${counter.count}',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
            const SizedBox(
              height: 20,
            ),
            ElevatedButton(
                onPressed: () {
                  counter.increment();
                },
                child: const Text("counter++")),
            ElevatedButton(
                onPressed: () {
                  counter.decrement();
                },
                child: const Text("counter--")),
          ],
        ),
      ),
    );
  }
}
